package com.siyuanren.gcocp.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.siyuanren.gcocp.common.util.MD5Utils;
import com.siyuanren.gcocp.common.web.bean.Result;
import com.siyuanren.gcocp.dto.ContentDTO;
import com.siyuanren.gcocp.service.ContentService;
import com.siyuanren.gcocp.service.VideoService;
import com.siyuanren.gcocp.util.VaildUtil;
import com.siyuanren.gcocp.vo.QueryChildContentVO;
import com.siyuanren.gcocp.vo.QueryContentVO;
import com.siyuanren.gcocp.web.wx.BaseWxController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by   anxing
 * Project_name gcocp-parent
 * Path         com.siyuanren.gcocp.web
 * Date         2018/4/18
 * Time         13:51
 * Description 课程内容
 */
@Slf4j
@Controller
@RequestMapping("/content")
public class ContentController extends BaseWxController {
    @Autowired
    ContentService contentService;
    @Autowired
    VideoService videoService;

    /**
     * 返回课程内容列表
     * @param queryContentVO
     * @return
     */
    @ResponseBody
    @RequestMapping("contentList")
    public Result getContentList(QueryContentVO queryContentVO) throws Exception {
        VaildUtil.check(queryContentVO);
        queryContentVO.setUserId(getUserId());
        List<ContentDTO> contentList = contentService.getContentList(queryContentVO);
        return new Result(contentList);
    }
    /**
     * 返回子课程内容列表. 用于嵌套的子课程内容列表
     * @param queryChildContentVO
     * @return
     */
    @ResponseBody
    @RequestMapping("childList")
    public Result getChildContentList(QueryChildContentVO queryChildContentVO) throws Exception {
        VaildUtil.check(queryChildContentVO);
        queryChildContentVO.setUserId(getUserId());
        List<ContentDTO> childContentList = contentService.getChildContentList(queryChildContentVO);
        return new Result(childContentList);

    }

    @ResponseBody
    @RequestMapping("getAudioUrl")
    public Result getAudioUrl(String audioId) throws Exception {
        String audioUrl = videoService.getAudioUrl(audioId);
        if (audioUrl != null) {
            return new Result(audioUrl);
        } else {
            return new Result(-1, "获取音频失败");
        }
    }
}
